package com.artifact.services.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.artifact.dao.GoodsInfoDao;
import com.artifact.dao.YungouPublishDao;
import com.artifact.model.GoodsInfo;
import com.artifact.model.YungouPublish;
import com.artifact.services.YungouPublishAjaxServices;
import com.artifact.yungou.CacheData;
import com.google.gson.Gson;

@Service("yungouPublishAjaxServices")
public class YungouPublishAjaxServicesImpl implements YungouPublishAjaxServices {
    private YungouPublishDao yungouPublishDao;
    private GoodsInfoDao goodsInfoDao;

    @Resource(name = "yungouPublishDao")
    public void setYungouPublishDao(YungouPublishDao yungouPublishDao) {
        this.yungouPublishDao = yungouPublishDao;
    }

    @Resource(name = "goodsInfoDao")
    public void setGoodsInfoDao(GoodsInfoDao goodsInfoDao) {
        this.goodsInfoDao = goodsInfoDao;
    }

    @Override
    public String addYungouPublishs(int goodsID, int codePeriod, int codeID, int codeRNO, int awardPosition,
                                    String userName, int buyCount, String buyStartPosition) {
        YungouPublish yungouPublish = new YungouPublish(goodsID, codePeriod, codeID, codeRNO, awardPosition,
                userName, buyCount, buyStartPosition);
        CacheData.setYungouPublishCacheDate(goodsID, codePeriod, yungouPublish);
        if (yungouPublishDao.getYungouPublish(goodsID, codePeriod) == null) {
            yungouPublishDao.addYungouPublish(goodsID, codePeriod, codeID, codeRNO, awardPosition, userName, buyCount,
                    buyStartPosition);
        }
        return null;
    }

    @Override
    public String getYungouPublishs(int goodsID, int codePeriod, int selectNum) {
        ArrayList<String[]> stringList = new ArrayList<String[]>();
        try {
            if (codePeriod == 0) {
                ConcurrentSkipListMap<Integer, YungouPublish> yungouPublishCacheDateMap = CacheData
                        .getYungouPublishCacheDate(goodsID);
                if (yungouPublishCacheDateMap == null || yungouPublishCacheDateMap.size() < selectNum) {
                    List<YungouPublish> YungouPublishList = yungouPublishDao.getYungouPublishs(goodsID, selectNum);
                    for (YungouPublish yungouPublish : YungouPublishList) {
                        if (yungouPublish.getAwardPosition() > 0 && yungouPublish.getBuyCount() > 0) {
                            stringList.add(yungouPublish.getYungouPublishInfo());
                            CacheData.setYungouPublishCacheDate(goodsID, yungouPublish.getCodePeriod(), yungouPublish);
                        } else {
                            yungouPublishDao.deleteYungouPublish(goodsID, yungouPublish.getCodePeriod());
                            CacheData.removeYungouPublishCacheDate(goodsID, yungouPublish.getCodePeriod());
                        }
                    }
                } else {
                    Iterator<Entry<Integer, YungouPublish>> it = yungouPublishCacheDateMap.entrySet().iterator();
                    for (int k = 0; it.hasNext() && k < selectNum; k++) {
                        Entry<Integer, YungouPublish> entry = it.next();
                        // System.out.println("数据" +
                        // entry.getValue().getCodePeriod());
                        stringList.add(entry.getValue().getYungouPublishInfo());
                    }
                }
            } else {
                YungouPublish yungouPublish = CacheData.getYungouPublishCacheDate(goodsID, codePeriod);
                if (yungouPublish == null) {
                    ConcurrentSkipListMap<Integer, YungouPublish> yungouPublishCacheDateMap = CacheData
                            .getYungouPublishCacheDate(goodsID);
                    if (yungouPublishCacheDateMap.lastKey() > codePeriod) {
                        yungouPublish = yungouPublishDao.getYungouPublish(goodsID, codePeriod);
                        if (yungouPublish != null) {
                            CacheData.setYungouPublishCacheDate(goodsID, codePeriod, yungouPublish);
                        }
                    }
                }
                if (yungouPublish != null) {
                    if (yungouPublish.getAwardPosition() > 0 && yungouPublish.getBuyCount() > 0) {
                        stringList.add(yungouPublish.getYungouPublishInfo());
                    } else {
                        CacheData.removeYungouPublishCacheDate(goodsID, codePeriod);
                        yungouPublishDao.deleteYungouPublish(goodsID, codePeriod);
                    }
                }
            }
            String string = new Gson().toJson(stringList);
            return string;
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public void deleteYungouPublish(int goodsID, int codePeriod) {
        CacheData.removeYungouPublishCacheDate(goodsID, codePeriod);
        yungouPublishDao.deleteYungouPublish(goodsID, codePeriod);
    }

    @Override
    public void addGoodsInfo(int goodsID, String goodsSName, int codeID, int codeLimitBuy, String goodsPic,
                             int isSale) {
        if (goodsInfoDao.getGoodsInfo(goodsID) == null) {
            goodsInfoDao.addGoodsInfo(goodsID, goodsSName, codeID, codeLimitBuy, goodsPic, isSale);
        }
    }

    @Override
    public void setGoodsInfoIsSale(int goodsID, int isSale) {
        goodsInfoDao.setGoodsInfoIsSale(goodsID, isSale);
    }

    @Override
    public List<GoodsInfo> getGoodsInfosBySale() {
        return goodsInfoDao.getGoodsInfosBySale();
    }

    @Override
    public void addGoodsInfo(String goodsInfo) {
        String[] info = goodsInfo.split("__");
        if (CacheData.getGoodsInfoDate(info[0]) == null) {
            if ("1".equals(info[5])) {
                CacheData.setGoodsInfoDate(info[0], new GoodsInfo(Integer.parseInt(info[0]), info[1],
                        Integer.parseInt(info[2]), Integer.parseInt(info[3]), info[4], Integer.parseInt(info[5])));
            }
            addGoodsInfo(Integer.parseInt(info[0]), info[1], Integer.parseInt(info[2]), Integer.parseInt(info[3]),
                    info[4], Integer.parseInt(info[5]));
        } else {
            if ("0".equals(info[5])) {
                CacheData.removeGoodsInfoDate(info[0]);
            }
        }
    }

    @Override
    public String getGoodsInfos(int goodsID) {
        ArrayList<String[]> stringList = new ArrayList<String[]>();
        try {
            if (goodsID == 0) {
                ConcurrentHashMap<String, GoodsInfo> goodsInfoMap = CacheData.getGoodsInfoDate();
                for (GoodsInfo goodsInfo : goodsInfoMap.values()) {
                    stringList.add(goodsInfo.getGoodsInfo());
                }
            } else {
                GoodsInfo goodsInfo = goodsInfoDao.getGoodsInfo(goodsID);
                if (goodsInfo != null) {
                    stringList.add(goodsInfo.getGoodsInfo());
                }
            }
            return new Gson().toJson(stringList);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String getYungouPublishsNotData(int goodsID, int codePeriod) {
        try {
            ConcurrentSkipListMap<Integer, YungouPublish> yungouPublishCacheDateMap = CacheData.getYungouPublishCacheDate(goodsID);
            ArrayList<Integer> stringList = new ArrayList<Integer>();
            for (int i = codePeriod; i > codePeriod - 50 && i > 0; i--) {
                stringList.add(i);
            }
            if (yungouPublishCacheDateMap == null || yungouPublishCacheDateMap.size() < 50) {
                List<YungouPublish> YungouPublishList = yungouPublishDao.getYungouPublishs(goodsID, 50);
                for (YungouPublish yungouPublish : YungouPublishList) {
                    stringList.remove(new Integer(yungouPublish.getCodePeriod()));
                }
            } else {
                for (YungouPublish yungouPublish : yungouPublishCacheDateMap.values()) {
                    stringList.remove(new Integer(yungouPublish.getCodePeriod()));
                }
            }
            String string = new Gson().toJson(stringList);
            return string;
        } catch (Exception e) {
        }
        return null;
    }
}
